package two_shang._11_09;

import java.util.*;

public class Test04 {
    public static class Student {
        int[] arr = new int[10];

        private int id;
        private String name;
        private int age;
        private String className;

        public Student(int id, String name, int age, String className) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.className = className;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public String getClassName() {
            return className;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Student student = (Student) o;
            return id == student.id;
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", className='" + className + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student(1, "Alice", 18, "Class A"));
        studentList.add(new Student(2, "Bob", 19, "Class B"));
        studentList.add(new Student(3, "Alice", 20, "Class A"));
        studentList.add(new Student(1, "Alice", 18, "Class A")); // 重复的学生对象

        // 1) 抽取不重复的学生并打印
        Set<Student> uniqueStudents = new HashSet<>(studentList);
        System.out.println("不重复的学生列表：");
        for (Student student : uniqueStudents) {
            System.out.println(student);
        }

        // 2) 将学生及出现次数放入集合中以备高效查询
        Map<Student, Integer> studentCountMap = new HashMap<>();
        for (Student student : studentList) {
            studentCountMap.put(student, studentCountMap.getOrDefault(student, 0) + 1);
        }
        System.out.println("学生及出现次数的集合：");
        for (Map.Entry<Student, Integer> entry : studentCountMap.entrySet()) {
            System.out.println(entry.getKey() + " 出现次数：" + entry.getValue());
        }
    }
}
